package com.base.component.ai.workflow.handle;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.base.component.ai.agent.bo.ContextBO;
import com.base.component.ai.agent.bo.ContextParamBO;
import com.base.component.ai.workflow.core.FieldTypeEnum;
import com.base.component.ai.workflow.core.NodeHandle;
import com.base.component.ai.workflow.flow.Flow;
import com.base.component.ai.workflow.logs.NodeLog;
import com.base.component.ai.workflow.logs.NodeLogJdbcSelect;
import com.base.component.ai.workflow.node.Node;
import com.base.component.ai.workflow.node.NodeJdbcSelect;
import com.gitee.magic.core.json.JsonArray;
import com.gitee.magic.core.json.JsonObject;
import com.gitee.magic.jdbc.persistence.source.jdbc.EntityJdbcManager;
import com.gitee.magic.jdbc.persistence.source.jdbc.mapper.SqlTemplate;

@Component
public class NodeHandleJdbcSelect<MSG,AP extends ContextParamBO,PP> implements NodeHandle<MSG,AP,PP>{

	@Autowired
	private EntityJdbcManager entityJdbcManager;
	
	@Override
	public <T extends Node> boolean support(Class<T> cls) {
		return cls.equals( NodeJdbcSelect.class);
	}
	
	@Override
	public <T extends Node>List<Flow> execute(ContextBO<MSG,AP,PP> context,Map<String,Object> params,Flow flow,T node,Consumer<NodeLog> c) {
		NodeJdbcSelect d=(NodeJdbcSelect)node;
		Map<String,Object> sqlParams=new HashMap<>();
		for(String p:d.getParameterNames()) {
			sqlParams.put(p, params.get(p));
		}
		String sql=SqlTemplate.getMapperSqlTemplate(d.getSqlId(),sqlParams);
		String sqlStr=SqlTemplate.getExecuteSql(sql);
		Object[] sqlArgs=SqlTemplate.getExecuteParameter(sql, sqlParams);
		Object resultValue=null;
		if(d.getValueType()==FieldTypeEnum.String) {
			resultValue=entityJdbcManager.getJdbcTemplate().queryForObject(sqlStr,String.class, sqlArgs);
		}else if(d.getValueType()==FieldTypeEnum.Integer) {
			resultValue=entityJdbcManager.getJdbcTemplate().queryForObject(sqlStr,Integer.class, sqlArgs);
		}else if(d.getValueType()==FieldTypeEnum.Long) {
			resultValue=entityJdbcManager.getJdbcTemplate().queryForObject(sqlStr,Long.class, sqlArgs);
		}else if(d.getValueType()==FieldTypeEnum.Float) {
			resultValue=entityJdbcManager.getJdbcTemplate().queryForObject(sqlStr,Float.class, sqlArgs);
		}else if(d.getValueType()==FieldTypeEnum.Double) {
			resultValue=entityJdbcManager.getJdbcTemplate().queryForObject(sqlStr,Double.class, sqlArgs);
		}else if(d.getValueType()==FieldTypeEnum.JsonObject) {
			List<Map<String,Object>> result=entityJdbcManager.getListMapBySql(sqlStr, sqlArgs);
			for(int i=0;i<result.size();i++) {
				resultValue=new JsonObject(result.get(i));
			}
		}else if(d.getValueType()==FieldTypeEnum.JsonArray) {
			List<Map<String,Object>> result=entityJdbcManager.getListMapBySql(sqlStr, sqlArgs);
			JsonArray arrays=new JsonArray();
			for(int i=0;i<result.size();i++) {
				arrays.put(new JsonObject(result.get(i)));
			}
			resultValue=arrays;
		}
		
		NodeLogJdbcSelect nodeLog=new NodeLogJdbcSelect();
		nodeLog.setSqlParams(sqlParams);
		nodeLog.setResultValue(resultValue);
		c.accept(nodeLog);
		
		params.put(d.getVariableName(), resultValue);
		return flow.getNexts();
	}
	
}
